import config from "../config/index.js";
import utils from "../utils/index.js";
import qs from 'qs'
class Http {

	get(params) {

		uni.showLoading({
			title: '加载中'
		});
		return new Promise((success, error) => {

			uni.request({
				method: 'GET',
				url: utils.checkUrl() + params.url, //仅为示例，并非真实接口地址。
				data: qs.stringify(params.data),
				header: Object.assign(params.header, {
					token: uni.getStorageSync('token'),
					'X-Requested-With': 'XMLHttpRequest ', //自定义请求头信息
					'content-type': 'application/x-www-form-urlencoded',
				}),
				success: (res) => {
					success(this.HttpInterceptor(res.data))
				},
				fail: (err) => {
					error(err)
				}
			});
		})
	}

	post(params) {

		// console.log(params);
		if (params.url == config.urlList.setSettlementPrice) {
			uni.showLoading({
				title: '正在更新中，请稍等'
			})
		} else {
			uni.showLoading({
				title: '加载中'
			});
		}
		return new Promise((success, error) => {

			uni.request({
				method: 'POST',
				url: utils.checkUrl() + params.url, //仅为示例，并非真实接口地址。
				data: qs.stringify(params.data),
				header: Object.assign(params.header, {
					token: uni.getStorageSync('token'),
					'X-Requested-With': 'XMLHttpRequest ', //自定义请求头信息
					'content-type': 'application/x-www-form-urlencoded',
				}),
				success: (res) => {
					success(this.HttpInterceptor(res.data))
				},
				fail: (err) => {
					error(err)
				}
			});
		})
	}

	HttpInterceptor(result) {
		let type = uni.getStorageSync('type')
		// console.log(type);
		// console.log(result);
		switch (result.code) {
			case 1:
				uni.hideLoading();
				return result
				break;
			case 0:
				uni.hideLoading();
				// uni.showToast({
				// 	icon: 'none',
				// 	title: `${result.msg}`,
				// 	duration: 2000
				// });
				return result
				throw new Error(result.msg)
				break;
			case 401:
				uni.hideLoading();
				// if (type == 4) {

				// }
				uni.showToast({
					icon: 'none',
					title: `${result.msg}`,
					duration: 2000
				});
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login'
					})

				}, 1000)
				throw new Error(result.msg)
				break;
			case '201':

				uni.hideLoading();
				// uni.showToast({
				// 	icon: 'none',
				// 	title: `${result.msg}`,
				// 	duration: 2000
				// });
				console.log(result)
				return result
				throw new Error(result.msg)
				break;
			case '202':
				uni.hideLoading();
				return result
				break;
			case 101:
				// uni.showToast({
				// 	icon: 'none',
				// 	title: `${result.msg}`,
				// 	duration: 2000
				// });
				// setTimeout(() => {
				// 	uni.navigateTo({
				// 		url: '/pages/my/recharge'
				// 	})
				// }, 1000)
				return result
				throw new Error(result.msg)
				break;
				// case 201:
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: `${result.msg}`,
				// 		duration: 2000
				// 	});
				// 	setTimeout(()=>{
				// 		uni.navigateTo({
				// 			url: '/pages/my/recharge'
				// 		})
				// 	},2000)
				// 	throw new Error(result.msg)
				// 	break;
			case 102:
				uni.showToast({
					icon: 'none',
					title: `${result.msg}`,
					duration: 2000
				});
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/my/login/modify/modify'
					})
				}, 500)

				throw new Error(result.msg)
				break;
			case '500':
				uni.showToast({
					icon: 'none',
					title: `${result.msg}`,
					duration: 2000
				});
				throw new Error(result.msg)
				break;
		}
	}

}

export default new Http();